<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_model extends CI_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	//get all products
	public function get_all()
	{
		$this->db->select('p.id, p.name, p.discription, p.price, p.cate_id,'
			. 'ct.name as cate_name');
		$this->db->from('products p');
		$this->db->join('categories ct', 'p.cate_id = ct.id');
		$query = $this->db->get();

		return $query->result();
	}

	//get all categories
	public function get_products() {
		$query = $this->db->get('products');
		return $query->result();
	}

	//insert
	public function add_new($data) {
		if($this->db->insert('products', $data)) {
			$this->db->where($data);
			$res = $this->db->get('products')->result();

			return $res[0]->id;
		}else 
			return -1; //insert fail
	}

	//edit
	public function edit($data, $cond) {
		$this->db->where($cond);
		if($this->db->update('products', $data))
			return true;
		return false;
	}

	//delete
	public function delete($cond) {
		if($this->db->delete('products', $cond)) 
			return true;
		return false;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */